package com.cgq.pro.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.cgq.base.action.BaseAction;
import com.cgq.pro.entity.CityStatistics;
import com.cgq.pro.entity.Land;
import com.cgq.pro.entity.LandForm;
import com.cgq.pro.entity.Region;
import com.cgq.pro.entity.RegionStatistics;
import com.cgq.pro.service.CityService;
import com.cgq.pro.service.EcityService;
import com.cgq.pro.service.LandService;
import com.cgq.pro.service.RegionService;
import com.cgq.pro.service.RegionStaticService;
import com.cgq.utils.GlobalContext;
import com.cgq.utils.PropertiesUtil;

@Controller
public class MapAction extends BaseAction {
	
	@Autowired
	private CityService cityService;
	@Autowired
	private RegionStaticService regionStaticService;
	@Autowired
	private LandService landService;
	@Autowired
	private RegionService regionService;
	@Autowired
	private EcityService ecityService;
	
	/**获取地块方式*/
	private String viewType;
	/**编辑模式*/
	private String operateType;
	
	/**编号*/
	private String id;
	/**模块类型：1，市区统计模块；2，行政区统计模块;3,土地模块*/
	private int type;
	/**土地类型：1.用地计划2，土地出让3，土地成交*/
	private int ltype;
	/**市区统计实体*/
	private CityStatistics cityStatistics;
	/**行政区统计实体*/
	private RegionStatistics regionStatistics;
	
	private List<Region> regions;
	/**选中的标注名称*/
	private String markerName;
	
	private Land landEntity;
	
	private LandForm landForm;
	/**标注内容*/
	private String content;
	
	public List<Region> getRegions() {
		return regions;
	}

	public void setRegions(List<Region> regions) {
		this.regions = regions;
	}
	
	public Land getLandEntity() {
		return landEntity;
	}

	public void setLandEntity(Land landEntity) {
		this.landEntity = landEntity;
	}

	public String getViewType() {
		return viewType;
	}

	public void setViewType(String viewType) {
		this.viewType = viewType;
	}
	
	public String getOperateType() {
		return operateType;
	}

	public void setOperateType(String operateType) {
		this.operateType = operateType;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}
	
	public int getLtype() {
		return ltype;
	}

	public void setLtype(int ltype) {
		this.ltype = ltype;
	}

	public CityStatistics getCityStatistics() {
		return cityStatistics;
	}

	public void setCityStatistics(CityStatistics cityStatistics) {
		this.cityStatistics = cityStatistics;
	}

	public RegionStatistics getRegionStatistics() {
		return regionStatistics;
	}

	public void setRegionStatistics(RegionStatistics regionStatistics) {
		this.regionStatistics = regionStatistics;
	}
	
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getMarkerName() {
		return markerName;
	}

	public void setMarkerName(String markerName) {
		this.markerName = markerName;
	}

	public LandForm getLandForm() {
		return landForm;
	}

	public void setLandForm(LandForm landForm) {
		this.landForm = landForm;
	}

	/**
	 * 地图界面
	 * @return
	 */
	public String toList() throws Exception{
		regions=regionService.findAll();
		ecity=ecityService.getCurrentCity();
		StringBuffer sb=new StringBuffer();
		String sval="";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		if(viewType!=null && !"".equals(viewType) && viewType.equals("loc")){
			switch (type) {
			case 1: //市区统计
				cityStatistics= cityService.find(id);
				markerName=cityStatistics.getMarkerName();
				sb.append(cityStatistics.getLon()+"*"+cityStatistics.getLat()
						+"*"+cityStatistics.getName()+"*"+cityStatistics.getLandType()
						+"*"+cityStatistics.getPoints()+"*4*"); //temp代表占位符，为了和土地模块的字符串结构保持一致
				sval="<table>"+
						"<tr><td colspan='2'>开发公司："+cityStatistics.getCompany()+"</td></tr>"+
						"<tr><td style='width:150px;'>行政区域："+cityStatistics.getRegionId().getName()+"</td>"+
						"<td style='width:150px;'>物业类型："+PropertiesUtil.readByKey("landUseType", "type."+cityStatistics.getType())+"</td></tr>"+
						"<tr><td style='width:150px;'>户型："+cityStatistics.getHouseType()+"</td>"+
						"<td style='width:150px;'>户型面积："+cityStatistics.getHouseArea()+" 平米</td></tr>"+
						"<tr><td style='width:150px;'>总建面："+cityStatistics.getTotalArea()+ " 平米</td>"+
						"<td style='width:150px;'>总套数："+cityStatistics.getTotalTs()+"</td></tr>"+
						"<tr><td style='width:150px;'>楼栋数："+cityStatistics.getTotalBuilding()+"</td>"+
						"<td style='width:150px;'>已推盘面积："+cityStatistics.getYtpArea()+" 平米</td></tr>"+
						"<tr><td style='width:150px;'>已推盘套数："+cityStatistics.getYtpTs()+"</td>"+
						"<td style='width:150px;'>已售面积："+cityStatistics.getYsArea()+" 平米</td></tr>"+
						"<tr><td style='width:150px;'>已售套数："+cityStatistics.getYsTs()+"</td>"+
						"<td style='width:150px;'>销售均价："+cityStatistics.getAveragePrice()+" 元</td></tr>"+
						"<tr><td colspan='2'>楼盘状态："+PropertiesUtil.getPStatusStr(cityStatistics.getP_status())+"</td></tr>"+
						"<tr><td style='width:150px;'>创建时间："+sdf.format(cityStatistics.getDateCre())+"</td>"+
						"<td style='width:150px;'>创建人："+cityStatistics.getUserCre()+"</td></tr>"+
						"<tr><td colspan='2'>备注："+cityStatistics.getRemark()+"</td></tr>"+
					 "</table>";
				sb.append(sval);
				break;
			case 2: //行政区统计
				regionStatistics=regionStaticService.find(id);
				markerName=regionStatistics.getMarkerName();
				sb.append(regionStatistics.getLon()+"*"+regionStatistics.getLat()
						+"*"+regionStatistics.getName()+"*"+regionStatistics.getLandType()
						+"*"+regionStatistics.getPoints()+"*5*");
				sval="<table>"+
						"<tr><td style='width:150px;'>行政分区："+regionStatistics.getRegionId().getName()+"</td>"+
						"<td style='width:150px;'>物业类型："+PropertiesUtil.readByKey("landUseType", "type."+regionStatistics.getType())+"</td></tr>"+
						"<tr><td style='width:150px;'>成交套数："+regionStatistics.getCjts()+"</td>"+
						"<td style='width:150px;'>成交面积："+regionStatistics.getCjmj()+" 平米</td></tr>"+
						"<tr><td style='width:150px;'>成交均价："+regionStatistics.getCjjj()+" 元</td>"+
						"<td style='width:150px;'>楼盘状态："+PropertiesUtil.getPStatusStr(regionStatistics.getP_status())+"</td></tr>"+
						"<tr><td style='width:150px;'>创建时间："+sdf.format(regionStatistics.getDateCre())+"</td>"+
						"<td style='width:150px;'>创建人："+regionStatistics.getUserCre()+"</td></tr>"+
						"<tr><td colspan='2'>备注："+regionStatistics.getRemark()+"</td></tr>"+
					 "</table>";
				sb.append(sval);
				break;
			default: //土地模块相关
				landEntity=landService.find(id);
				markerName=landEntity.getMarkerName();
				sb.append(landEntity.getLng()+"*"+landEntity.getLat()
						+"*"+landEntity.getName()+"*"+landEntity.getLandType()
						+"*"+landEntity.getPoints()+"*"+landEntity.getType()+"*");
				if(landEntity.getType()==1){
					sval="<table>"+
							"<tr><td colspan='2'>坐落位置："+landEntity.getLocation()+"</td></tr>"+
							"<tr><td style='width:150px;'>所属区位："+landEntity.getRegion().getName()+"</td>"+
							"<td style='width:150px;'>土地用途："+landEntity.getUseType()+"</td></tr>"+
							"<tr><td style='width:150px;'>出让面积："+landEntity.getSellArea()+" 平米</td>"+
							"<td style='width:150px;'>规划指标："+landEntity.getQuote()+"</td></tr>"+
							"<tr><td style='width:150px;'>创建时间："+sdf.format(landEntity.getDateCre())+"</td>"+
							"<td style='width:150px;'>创建人："+landEntity.getUserCre()+"</td></tr>"+
							"<tr><td colspan='2'>备注："+landEntity.getRemark()+"</td></tr>"
							+"</table>";
				}else if(landEntity.getType()==2){
					sval="<table>"+
							"<tr><td colspan='2'>坐落位置："+landEntity.getLocation()+"</td></tr>"+
							"<tr><td style='width:150px;'>所属区位："+landEntity.getRegion().getName()+"</td>"+
							"<td style='width:150px;'>土地用途："+landEntity.getUseType()+"</td></tr>"+
							"<tr><td style='width:150px;'>出让面积："+landEntity.getSellArea()+" 平米</td>"+
							"<td style='width:150px;'>地块要点："+landEntity.getPoint()+"</td></tr>"+
							"<tr><td style='width:150px;'>挂牌起始价格："+landEntity.getStart_price()+" 元</td>"+
							"<td style='width:150px;'>挂牌时间："+sdf.format(landEntity.getList_date())+"</td></tr>"+
							"<tr><td style='width:150px;'>创建时间："+sdf.format(landEntity.getDateCre())+"</td>"+
							"<td style='width:150px;'>创建人："+landEntity.getUserCre()+"</td></tr>"+
							"<tr><td colspan='2'>备注："+landEntity.getRemark()+"</td></tr>"
							+"</table>";
				}else{
					sval="<table>"+
							"<tr><td colspan='2'>坐落位置："+landEntity.getLocation()+"</td></tr>"+
							"<tr><td style='width:150px;'>所属区位："+landEntity.getRegion().getName()+"</td>"+
							"<td style='width:150px;'>土地用途："+landEntity.getUseType()+"</td></tr>"+
							"<tr><td style='width:150px;'>土地面积："+landEntity.getSellArea()+" 平米</td>"+
							"<td style='width:150px;'>容积率："+landEntity.getCapacity()+"</td></tr>"+
							"<tr><td style='width:150px;'>起始价格："+landEntity.getInitPrice()+" 元</td>"+
							"<td style='width:150px;'>成交价格："+landEntity.getTransPrice()+" 元</td></tr>"+
							"<tr><td style='width:150px;'>成交日期："+sdf.format(landEntity.getTransDate())+"</td>"+
							"<td style='width:150px;'>竞得人："+landEntity.getBidder()+"</td></tr>"+
							"<tr><td style='width:150px;'>创建时间："+sdf.format(landEntity.getDateCre())+"</td>"+
							"<td style='width:150px;'>创建人："+landEntity.getUserCre()+"</td></tr>"+
							"<tr><td colspan='2'>备注："+landEntity.getRemark()+"</td></tr>"
							+"</table>";
				}
				sb.append(sval);
				break;
			}
		}
		content=sb.toString();
		return GlobalContext.RESULT_LIST_UI;
	}
	
	/**
	 * 获取坐标
	 * @return
	 */
	public String getLoc(){
		ecity=ecityService.getCurrentCity();
		return GlobalContext.RESULT_GETLOC;
	}
	
	public String locationUI(){
		regions=regionService.findAll();
		ecity=ecityService.getCurrentCity();
		StringBuffer sb=new StringBuffer();
		String sval="";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		try {
			if(viewType!=null && !"".equals(viewType) && viewType.equals("loc")){
				switch (type) {
				case 1: //市区统计
					cityStatistics= cityService.find(id);
					markerName=cityStatistics.getMarkerName();
					sb.append(cityStatistics.getLon()+"*"+cityStatistics.getLat()
							+"*"+cityStatistics.getName()+"*"+cityStatistics.getLandType()
							+"*"+cityStatistics.getPoints()+"*4*"); //temp代表占位符，为了和土地模块的字符串结构保持一致
					sval="<table>"+
							"<tr><td colspan='2'>开发公司："+cityStatistics.getCompany()+"</td></tr>"+
							"<tr><td style='width:150px;'>行政区域："+cityStatistics.getRegionId().getName()+"</td>"+
							"<td style='width:150px;'>物业类型："+PropertiesUtil.readByKey("landUseType", "type."+cityStatistics.getType())+"</td></tr>"+
							"<tr><td style='width:150px;'>户型："+cityStatistics.getHouseType()+"</td>"+
							"<td style='width:150px;'>户型面积："+cityStatistics.getHouseArea()+" 平米</td></tr>"+
							"<tr><td style='width:150px;'>总建面："+cityStatistics.getTotalArea()+" 平米</td>"+
							"<td style='width:150px;'>总套数："+cityStatistics.getTotalTs()+"</td></tr>"+
							"<tr><td style='width:150px;'>楼栋数："+cityStatistics.getTotalBuilding()+"</td>"+
							"<td style='width:150px;'>已推盘面积："+cityStatistics.getYtpArea()+" 平米</td></tr>"+
							"<tr><td style='width:150px;'>已推盘套数："+cityStatistics.getYtpTs()+"</td>"+
							"<td style='width:150px;'>已售面积："+cityStatistics.getYsArea()+" 平米</td></tr>"+
							"<tr><td style='width:150px;'>已售套数："+cityStatistics.getYsTs()+"</td>"+
							"<td style='width:150px;'>销售均价："+cityStatistics.getAveragePrice()+" 元</td></tr>"+
							"<tr><td colspan='2'>楼盘状态："+PropertiesUtil.getPStatusStr(cityStatistics.getP_status())+"</td></tr>"+
							"<tr><td style='width:150px;'>创建时间："+sdf.format(cityStatistics.getDateCre())+"</td>"+
							"<td style='width:150px;'>创建人："+cityStatistics.getUserCre()+"</td></tr>"+
							"<tr><td colspan='2'>备注："+cityStatistics.getRemark()+"</td></tr>"+
						 "</table>";
					sb.append(sval);
					break;
				case 2: //行政区统计
					regionStatistics=regionStaticService.find(id);
					markerName=regionStatistics.getMarkerName();
					sb.append(regionStatistics.getLon()+"*"+regionStatistics.getLat()
							+"*"+regionStatistics.getName()+"*"+regionStatistics.getLandType()
							+"*"+regionStatistics.getPoints()+"*5*");
					sval="<table>"+
							"<tr><td style='width:150px;'>行政分区："+regionStatistics.getRegionId().getName()+"</td>"+
							"<td style='width:150px;'>物业类型："+PropertiesUtil.readByKey("landUseType", "type."+regionStatistics.getType())+"</td></tr>"+
							"<tr><td style='width:150px;'>成交套数："+regionStatistics.getCjts()+"</td>"+
							"<td style='width:150px;'>成交面积："+regionStatistics.getCjmj()+" 平米</td></tr>"+
							"<tr><td style='width:150px;'>成交均价："+regionStatistics.getCjjj()+" 元</td>"+
							"<td style='width:150px;'>楼盘状态："+PropertiesUtil.getPStatusStr(regionStatistics.getP_status())+"</td></tr>"+
							"<tr><td style='width:150px;'>创建时间："+sdf.format(regionStatistics.getDateCre())+"</td>"+
							"<td style='width:150px;'>创建人："+regionStatistics.getUserCre()+"</td></tr>"+
							"<tr><td colspan='2'>备注："+regionStatistics.getRemark()+"</td></tr>"+
						 "</table>";
					sb.append(sval);
					break;
				default: //土地模块相关
					landEntity=landService.find(id);
					markerName=landEntity.getMarkerName();
					sb.append(landEntity.getLng()+"*"+landEntity.getLat()
							+"*"+landEntity.getName()+"*"+landEntity.getLandType()
							+"*"+landEntity.getPoints()+"*"+landEntity.getType()+"*");
					if(landEntity.getType()==1){
						sval="<table>"+
								"<tr><td colspan='2'>坐落位置："+landEntity.getLocation()+"</td></tr>"+
								"<tr><td style='width:150px;'>所属区位："+landEntity.getRegion().getName()+"</td>"+
								"<td style='width:150px;'>土地用途："+landEntity.getUseType()+"</td></tr>"+
								"<tr><td style='width:150px;'>出让面积："+landEntity.getSellArea()+" 平米</td>"+
								"<td style='width:150px;'>规划指标："+landEntity.getQuote()+"</td></tr>"+
								"<tr><td style='width:150px;'>创建时间："+sdf.format(landEntity.getDateCre())+"</td>"+
								"<td style='width:150px;'>创建人："+landEntity.getUserCre()+"</td></tr>"+
								"<tr><td colspan='2'>备注："+landEntity.getRemark()+"</td></tr>"
								+"</table>";
					}else if(landEntity.getType()==2){
						sval="<table>"+
								"<tr><td colspan='2'>坐落位置："+landEntity.getLocation()+"</td></tr>"+
								"<tr><td style='width:150px;'>所属区位："+landEntity.getRegion().getName()+"</td>"+
								"<td style='width:150px;'>土地用途："+landEntity.getUseType()+"</td></tr>"+
								"<tr><td style='width:150px;'>出让面积："+landEntity.getSellArea()+" 平米</td>"+
								"<td style='width:150px;'>地块要点："+landEntity.getPoint()+"</td></tr>"+
								"<tr><td style='width:150px;'>挂牌起始价格："+landEntity.getStart_price()+" 元</td>"+
								"<td style='width:150px;'>挂牌时间："+sdf.format(landEntity.getList_date())+"</td></tr>"+
								"<tr><td style='width:150px;'>创建时间："+sdf.format(landEntity.getDateCre())+"</td>"+
								"<td style='width:150px;'>创建人："+landEntity.getUserCre()+"</td></tr>"+
								"<tr><td colspan='2'>备注："+landEntity.getRemark()+"</td></tr>"
								+"</table>";
					}else{
						sval="<table>"+
								"<tr><td colspan='2'>坐落位置："+landEntity.getLocation()+"</td></tr>"+
								"<tr><td style='width:150px;'>所属区位："+landEntity.getRegion().getName()+"</td>"+
								"<td style='width:150px;'>土地用途："+landEntity.getUseType()+"</td></tr>"+
								"<tr><td style='width:150px;'>土地面积："+landEntity.getSellArea()+" 平米</td>"+
								"<td style='width:150px;'>容积率："+landEntity.getCapacity()+"</td></tr>"+
								"<tr><td style='width:150px;'>起始价格："+landEntity.getInitPrice()+" 元</td>"+
								"<td style='width:150px;'>成交价格："+landEntity.getTransPrice()+" 元</td></tr>"+
								"<tr><td style='width:150px;'>成交日期："+sdf.format(landEntity.getTransDate())+"</td>"+
								"<td style='width:150px;'>竞得人："+landEntity.getBidder()+"</td></tr>"+
								"<tr><td style='width:150px;'>创建时间："+sdf.format(landEntity.getDateCre())+"</td>"+
								"<td style='width:150px;'>创建人："+landEntity.getUserCre()+"</td></tr>"+
								"<tr><td colspan='2'>备注："+landEntity.getRemark()+"</td></tr>"
								+"</table>";
					}
					sb.append(sval);
					break;
				}
			}
		} catch (Exception e) {
		}
		content=sb.toString();
		return "locationUI";
	}
	
	/**
	 * 修改地图标注图标
	 * @return
	 */
	public String modifyMarker(){
		return "markerUI";
	}
	
	public void getAjaxLand(){
		PrintWriter out;
		HttpServletResponse response=ServletActionContext.getResponse();
		StringBuffer sb=new StringBuffer();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String sval="";
		List<CityStatistics> cityList=cityService.getAllRecords(landForm);
		List<RegionStatistics> regionList=regionStaticService.getAllRecords(landForm);
		try {
			for (CityStatistics entity : cityList) {
				sb.append(entity.getLon()+"*"+entity.getLat()
						+"*"+entity.getName()+"*"+entity.getLandType()
						+"*"+entity.getPoints()+"*4*");
				sval="<table>"+
						"<tr><td colspan='2'>开发公司："+entity.getCompany()+"</td></tr>"+
						"<tr><td style='width:150px;'>行政区域："+entity.getRegionId().getName()+"</td>"+
						"<td style='width:150px;'>物业类型："+PropertiesUtil.readByKey("landUseType", "type."+entity.getType())+"</td></tr>"+
						"<tr><td style='width:150px;'>户型："+entity.getHouseType()+"</td>"+
						"<td style='width:150px;'>户型面积："+entity.getHouseArea()+" 平米</td></tr>"+
						"<tr><td style='width:150px;'>总建面："+entity.getTotalArea()+" 平米</td>"+
						"<td style='width:150px;'>总套数："+entity.getTotalTs()+"</td></tr>"+
						"<tr><td style='width:150px;'>楼栋数："+entity.getTotalBuilding()+"</td>"+
						"<td style='width:150px;'>已推盘面积："+entity.getYtpArea()+" 平米</td></tr>"+
						"<tr><td style='width:150px;'>已推盘套数："+entity.getYtpTs()+"</td>"+
						"<td style='width:150px;'>已售面积："+entity.getYsArea()+" 平米</td></tr>"+
						"<tr><td style='width:150px;'>已售套数："+entity.getYsTs()+"</td>"+
						"<td style='width:150px;'>销售均价："+entity.getAveragePrice()+" 元</td></tr>"+
						"<tr><td colspan='2'>楼盘状态："+PropertiesUtil.getPStatusStr(entity.getP_status())+"</td></tr>"+
						"<tr><td style='width:150px;'>创建时间："+sdf.format(entity.getDateCre())+"</td>"+
						"<td style='width:150px;'>创建人："+entity.getUserCre()+"</td></tr>"+
						"<tr><td colspan='2'>备注："+entity.getRemark()+"</td></tr>"+
					 "</table>";
				sb.append(sval+"*"+entity.getMarkerName()+"#");
			}
			for (RegionStatistics entity : regionList) {
				sb.append(entity.getLon()+"*"+entity.getLat()
						+"*"+entity.getName()+"*"+entity.getLandType()
						+"*"+entity.getPoints()+"*5*");
				sval="<table>"+
						"<tr><td style='width:150px;'>行政分区："+entity.getRegionId().getName()+"</td>"+
						"<td style='width:150px;'>物业类型："+PropertiesUtil.readByKey("landUseType", "type."+entity.getType())+"</td></tr>"+
						"<tr><td style='width:150px;'>成交套数："+entity.getCjts()+"</td>"+
						"<td style='width:150px;'>成交面积："+entity.getCjmj()+" 平米</td></tr>"+
						"<tr><td style='width:150px;'>成交均价："+entity.getCjjj()+" 元</td>"+
						"<td style='width:150px;'>楼盘状态："+PropertiesUtil.getPStatusStr(entity.getP_status())+"</td></tr>"+
						"<tr><td style='width:150px;'>创建时间："+sdf.format(entity.getDateCre())+"</td>"+
						"<td style='width:150px;'>创建人："+entity.getUserCre()+"</td></tr>"+
						"<tr><td colspan='2'>备注："+entity.getRemark()+"</td></tr>"+
					 "</table>";
				sb.append(sval+"*"+entity.getMarkerName()+"#");
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		try {
			out = response.getWriter();
			response.setContentType("text/html;charset=gb2312");
			String s=sb.toString();
			s=s.substring(0,s.length()-1);
			out.print(s);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
